package com.example.demo;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.adapter.MyAdapter;
import com.example.demo.model.Joke;
import com.example.demo.service.JokeService;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    TextView mtxthome;
    TextView mtxtpio;
    TextView mtxtuser;

    RecyclerView recyclerView;

    private JokeService jokeService;



    private ArrayList<Joke> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //隐藏标题栏
        ActionBar action = getSupportActionBar();
        if(action != null){
            action.hide();
        }
        //加载布局
        bindView();

        //顶部组件的点击事件 跳转页面
        Onclicklistener();
        //加载数据 访问后端拿到数据
        jokeService = new JokeService();
        jokeService.allJoke(new MainHandler());

    }


    //UI组件初始化
    private void bindView() {
        mtxthome = findViewById(R.id.txt_home);
        mtxtpio = findViewById(R.id.txt_poi);
        mtxtuser = findViewById(R.id.txt_user);
    }
    private void recycler(){
        //加载recycler控件
        recyclerView = findViewById(R.id.recycler_view);
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //设置适配器
        MyAdapter adapter = new MyAdapter(list);
        recyclerView.setAdapter(adapter);
    }
    //组件的点击事件
    private void Onclicklistener() {
        //主页点击事件
        mtxthome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //动态的点击事件
        mtxtpio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,TalkActivity.class);
                startActivity(intent);
            }
        });
        //用户的点击事件
        mtxtuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里需要判断用户是否登录 如果已经登录

                Intent intent = new Intent(MainActivity.this,PersonalActivity.class);
                startActivity(intent);

            }
        });

    }

    private class MainHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            String result = msg.getData().getString("result");

            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
           // list = json.get("data").getAsJsonArray();
            JsonArray map = json.get("data").getAsJsonArray();
            JsonObject jsonObject;
            for (int i=0;i<map.size();i++){
                jsonObject= map.get(i).getAsJsonObject();
                String title = jsonObject.get("title").getAsString();
                String content = jsonObject.get("content").getAsString();
                Joke mlist = new Joke(title,content);
                list.add(mlist);
            }
            recycler();
        }
    }

}
